package example.facility;

import example.annotation.processor.CompileProcessor;

import java.util.Objects;

public class Printer<T> {
    private volatile Boolean searched = Boolean.FALSE;
    private Object delegate;
    public String show(T t) {
        if (!searched) {
            synchronized (searched) {
                if (!searched) {
                    Class<?> aClass = t.getClass();
                    String target = aClass.getCanonicalName() + CompileProcessor.compiledClassSuffix;
                    try {
                        delegate = aClass.getClassLoader().loadClass(target).getDeclaredConstructor().newInstance();
                    } catch (Exception ignore) { }
                    searched = Boolean.TRUE;
                }
            }
        }
        if (Objects.isNull(delegate)) {
            return t.toString();
        } else {
            return delegate.toString();
        }
    }
}
